﻿Imports System
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Imports Microsoft.Reporting.WebForms

Partial Class Reports_ReportCard
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            With ReportViewer1
                .Reset()
            End With
        Else
            With ReportViewer1
                divRegions.CssClass = "R"
            End With
        End If

        ''Display the PDF in this window
        'Dim bytes() As Byte = ReportViewer1.LocalReport.Render("PDF", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
        'Response.Buffer = True
        'Response.Clear()
        'Response.ContentType = "application/pdf"
        ''Response.AddHeader("content-disposition", "attachment; filename=myfile." + extension)
        'Response.BinaryWrite(bytes)
        'Response.Flush()
        'Response.End()
    End Sub

    Protected Sub ddlRegions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegions.SelectedIndexChanged
        If ddlRegions.SelectedIndex < 1 Then Exit Sub


        If ddlRegions.SelectedValue <> "ALL REGIONS" Then
            SqlDataSource1.SelectCommand &= String.Format(" WHERE AREA_MGR='{0}'", ddlRegions.SelectedValue)
        End If
        Dim srcRpt = SqlDataSource1.Select(DataSourceSelectArguments.Empty)
        Dim rds As ReportDataSource = New ReportDataSource("DataSet1", srcRpt)

        With ReportViewer1
            .Reset()
            .ProcessingMode = ProcessingMode.Local
            Dim rpa() As ReportParameter = _
           { _
                   New ReportParameter("CacheDate", (New Caching()).CacheDate), _
                   New ReportParameter("Region", ddlRegions.SelectedValue) _
           }

            With .LocalReport
                .EnableHyperlinks = True
                If ddlRegions.SelectedValue = "ALL REGIONS" Then
                    .ReportPath = "reports\SSRS\TeamReportCard.rdlc"
                Else
                    .ReportPath = "reports\SSRS\OneTeamReportCard.rdlc"
                End If
                .SetParameters(rpa)
                .DataSources.Clear()
                .DataSources.Add(rds)
                .Refresh()
            End With
        End With
    End Sub
End Class
